<?php
class AlbumPic extends AlbumAppModel {
	var $useTable = 'sys_photos';
	var $validate = array (
			'title' => array (
					'rule' => 'notEmpty',
					'message' => '请输入图片名' 
			)
	);
	var $belongsTo = array(
			'Album' => array(
					'className' => 'Album.Album',
					'foreignKey' => 'album_id'
			));
	
	/**
	 * 根据用户id和相册id获取相册图片列表
	 * @author lj@gootop.net  
	 * @Date 2012-11-16
	 * @param
	 * @return
	 */
	function getAlbumPicListByUidAndAlbumId($userId, $AlbumId) {
		$AlbumPicList = $this->find ( 'all', array (
			'conditions' => array (
					'AlbumPic.user_id' => $userId,
					'AlbumPic.album_id' => $AlbumId 
			),
			'order' => 'AlbumPic.id DESC' 
		) );
		return $AlbumPicList;
	}
	/**
	 * 根据图片id获取图片信息
	 * @author lj@gootop.net  
	 * @Date 2012-11-16
	 * @param
	 * @return 
	 */
	function getAlbumPicById($userId,$id) {
		$list = $this->find ( 'first', array (
				'conditions' => array (
						'AlbumPic.user_id' => $userId,
						'AlbumPic.id' => $id
				)
		) );
		return $list;
	}
	/**
	 * 根据用户id和相册id统计图片数量
	 * @author lj@gootop.net  
	 * @Date 2012-11-27
	 * @param
	 * @return 
	 */
	function getPicsCountByAlbumId($userId, $AlbumId){
		$picsCount = $this->find ( 'count', array (
				'conditions' => array (
						'AlbumPic.user_id' => $userId,
						'AlbumPic.album_id' => $AlbumId
				)));
		return $picsCount;
	}
	/**
	 * 生成 图片路径
	 * @author zmx@gootop.net  
	 * @Date 2012-6-20
	 * @param $type (1:logo,2:logo_thumb,3:logo_cut,0:case)
	 * @return 
	 */
	function imagePath($albumPic) {
		$path = md5($albumPic['AlbumPic']['user_id'].'\\'.date('Ymdhis').$albumPic['AlbumPic']['album_id'].'*'.$albumPic['AlbumPic']['file']['name'].':');
		$floadF = substr($path, 0, 2);
		$filePath = 'static'.DS.$floadF.DS;
		return array('name'=>$path,'path'=>$filePath);
	}
	
	/**
	 * 图片总大小
	 * @author zmx@gootop.net  
	 * @Date 2012-11-20
	 * @param
	 * @return 
	 */
	function findAllSize($user_id) {
		$size = $this->find('first',array(
			'fields'=>array('SUM(AlbumPic.file_size) AS size'),
			'conditions'=>array(
				'AlbumPic.user_id'=>$user_id
			)
		));
		return $size[0];
	}
	
	/**
	 * 上传图片
	 * @author zmx@gootop.net  
	 * @Date 2012-11-20
	 * @param AlbumPic
	 * @return Array;
	 */
	function addNewPic($albumPic){
		if (!isset($albumPic['AlbumPic']['file'])) {
			$state = false;$msg = 'no pic upload';$return = '';
		}else{
			$size = $this->findAllSize($albumPic['AlbumPic']['user_id']);
			if (($size['size']+$albumPic['AlbumPic']['file']['size'])>Configure::read('Album.size')) {
				return array('state'=>false,'msg'=>'you album is full','data'=>'');
			}
			App::import('Vendor','Album.FileUpload',array('file' => 'FileUpload' . DS . 'fileUpload.php'));
			$filePath = $this->imagePath($albumPic);
			$albumPic['AlbumPic']['save_name'] = $filePath['name']; 
			$albumPic['AlbumPic']['save_path'] = $filePath['path']; 
			$albumPic['AlbumPic']['file_name'] = $albumPic['AlbumPic']['file']['name']; 
			$albumPic['AlbumPic']['title'] = !empty($albumPic['AlbumPic']['title'])?$albumPic['AlbumPic']['title']:$albumPic['AlbumPic']['file']['name'];
			$FU = new uploadFile($albumPic,Configure::read('Album.allow'));
			$state =  $FU->upload_file();
			$return = array();
			if($state){
				$msg = str_replace(DS,"/",$FU->uploaded);
				$albumPic['AlbumPic']['file_path']= $msg;
				$albumPic['AlbumPic']['file_size']= $albumPic['AlbumPic']['file']['size'];
				$albumPic['AlbumPic']['file_type']= $albumPic['AlbumPic']['file']['type'];
				$return = $this->save($albumPic);
				if(!$return){
					@unlink(WWW_ROOT.$FU->uploaded);
				}else{
					$return['photo']['info'] = $FU->info;
					$return['photo']['ext'] = $FU->ext;
					unset($return['AlbumPic']['file']);
				}
			}else{
				$msg = $FU->errmsg;
			};
		}
		return array('state'=>$state,'msg'=>$msg,'data'=>$return);
	}
	/**
	 * 删除图片
	 * @author lj@gootop.net  
	 * @Date 2012-11-27
	 * @param
	 * @return 
	 */
	function deleteNewsPic($userId,$id){
		$return = array('state'=>false,'msg'=>null,'data'=>null);
		$albumPic = $this->getAlbumPicById ($userId,$id);
		if($userId == $albumPic['AlbumPic']['user_id']){
			$file_path = str_replace(DS,'/',WWW_ROOT.'img'.DS.$albumPic['AlbumPic']['file_path']);
			$del = $this->delete($id);
			if($del){
				if (file_exists($file_path)) {
					@unlink($file_path);
				}
				$return['state'] = true;
				$return['msg'] = $albumPic['AlbumPic']['title'].'删除成功！';
			}else{
				$return['state'] = false;
				$return['msg'] = $albumPic['AlbumPic']['title'].'删除失败！';
			}
			
		}else{
			$return['state'] = false;
			$return['msg'] = '非法操作！';
		}
		return $return;
	}
	
	/**
	 * 图片剪切
	 * @author zmx@gootop.net  
	 * @Date 2012-12-6
	 * @param
	 * @return 
	 */
	public function photocut($src,$x,$y,$w,$h,$zoom,$type,$cut){
		App::import('Vendor','Album.PhotoCut',array('file' => 'FileUpload' . DS . 'PhotoCut.php'));
		$PC = new PhotoCut();
		return $PC->crop($src,$x,$y,$w,$h,$zoom,$type,$cut);
	}
}